package com.leech.jcrop.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Decoder;

public class FlashServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public FlashServlet() {
       
    }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		String rootPath = request.getSession().getServletContext().getRealPath("/");

		String savePath = rootPath+File.separator;  //保存图片路径 可以修改
		//String savePath = rootPath+File.separator+"upload"+ File.separator;  //保存图片路径 可以修改
		long savePicName = new Date().getTime();

		String file_src = savePath + savePicName  + "_src.jpg";    //保存原图
		String filename162 = savePath + savePicName + "_162.jpg";  //保存162
		String filename48 = savePath + savePicName + "_48.jpg";  //保存48
		String filename20 = savePath + savePicName + "_20.jpg";  //保存20

		String pic=request.getParameter("pic");
		String pic1=request.getParameter("pic1");
		String pic2=request.getParameter("pic2");
		String pic3=request.getParameter("pic3");

		if(!pic.equals("")&&pic!=null){
			//原图
			File file = new File(file_src);
			FileOutputStream fout = null;
			fout = new FileOutputStream(file);
			fout.write(new BASE64Decoder().decodeBuffer(pic));
			fout.close();
		}

		//图1
		File file1 = new File(filename162);
		FileOutputStream fout1 = null;
		fout1 = new FileOutputStream(file1);
		fout1.write(new BASE64Decoder().decodeBuffer(pic1));
		fout1.close();

		//图2
		File file2 = new File(filename48);
		FileOutputStream fout2 = null;
		fout2 = new FileOutputStream(file2);
		fout2.write(new BASE64Decoder().decodeBuffer(pic2));
		fout2.close();

		//图3
		File file3 = new File(filename20);
		FileOutputStream fout3 = null;
		fout3 = new FileOutputStream(file3);
		fout3.write(new BASE64Decoder().decodeBuffer(pic3));
		fout3.close();

		String picUrl = savePath+savePicName;
		out.println("{\"status\":1,\"picUrl\":\""+picUrl+"\"}"); //返回图片地址
       
	}

}
